package com.lu.pojo.network.udp.chat;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;


/**
 * @author luzhenfang
 * @date 2020 05 18
 * @description udp实例：聊天模块 服务端
 */
public class UdpServer {
    public static void main(String[] args) throws IOException {
        DatagramSocket socket = new DatagramSocket(6666);

        while (true) {
            // 准备接收
            byte[] buffer = new byte[1024];
            DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
            socket.receive(packet);
            // 断开连接  bye
            byte[] data = packet.getData();
            String reciveData = new String(data, 0, packet.getLength());
            System.out.println(reciveData);
            if(reciveData.equals("bye")){
                break;
            }
            // 阻塞接收
        }
        socket.close();
    }
}
